<template>
  <div class="activity-admin">
    <div class="page-header">
      <h1>学术活动审核</h1>
      <div class="filters">
        <el-select v-model="q.type" placeholder="活动类型" clearable style="width: 140px;">
          <el-option label="学术会议" value="学术会议" />
          <el-option label="教学研讨会" value="教学研讨会" />
          <el-option label="培训" value="培训" />
          <el-option label="考察交流" value="考察交流" />
          <el-option label="竞赛评审" value="竞赛评审" />
          <el-option label="学术讲座" value="学术讲座" />
        </el-select>
        <el-select v-model="q.role" placeholder="参与角色" clearable style="width: 140px; margin-left: 12px;">
          <el-option label="组织者" value="组织者" />
          <el-option label="主持人" value="主持人" />
          <el-option label="报告人" value="报告人" />
          <el-option label="参会代表" value="参会代表" />
          <el-option label="评委" value="评委" />
        </el-select>
        <el-select v-model="q.organizerLevel" placeholder="主办级别" clearable style="width: 140px; margin-left: 12px;">
          <el-option label="校级" value="校级" />
          <el-option label="市厅级" value="市厅级" />
          <el-option label="省部级" value="省部级" />
          <el-option label="国家级" value="国家级" />
          <el-option label="国际级" value="国际级" />
        </el-select>
        <el-date-picker v-model="q.range" type="daterange" range-separator="至" start-placeholder="开始" end-placeholder="结束" value-format="YYYY-MM-DD" style="margin-left: 12px;" />
        <el-button type="primary" :loading="loading" style="margin-left: 12px;" @click="fetchAll">查询</el-button>
      </div>
    </div>

    <!-- 统计卡片 -->
    <el-row :gutter="12" class="stats-row">
      <el-col :span="6"><el-card class="stat"><div>总数</div><div class="num">{{ stats.total }}</div></el-card></el-col>
      <el-col :span="6"><el-card class="stat"><div>审核中</div><div class="num info">{{ stats.pending }}</div></el-card></el-col>
      <el-col :span="6"><el-card class="stat"><div>已驳回</div><div class="num danger">{{ stats.rejected }}</div></el-card></el-col>
      <el-col :span="6"><el-card class="stat"><div>已通过</div><div class="num success">{{ stats.approved }}</div></el-card></el-col>
    </el-row>

    <!-- 列表 -->
    <el-card>
      <el-table :data="rows" stripe v-loading="loading">
        <el-table-column prop="participantName" label="参与人" width="100" />
        <el-table-column prop="activityName" label="活动名称" min-width="220"/>
        <el-table-column prop="activityType" label="类型" width="110" />
        <el-table-column prop="organizer" label="主办单位" width="150" />
        <el-table-column prop="organizerLevel" label="主办级别" width="110" />
        <el-table-column label="时间" width="220">
          <template #default="{ row }">{{ d(row.startDate) }} 至 {{ d(row.endDate) }}</template>
        </el-table-column>
        <el-table-column prop="participantRole" label="角色" width="100" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="tag(row.status)">{{ statusText(row.status) }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="240" fixed="right">
          <template #default="{ row }">
            <el-button type="text" @click="openAudit(row, '已通过')">通过</el-button>
            <el-button type="text" style="color:#f56c6c;" @click="openAudit(row, '已驳回')">驳回</el-button>
            <el-button type="text" @click="view(row)">查看</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div style="text-align:right;margin-top:12px;">
        <el-pagination background layout="prev, pager, next, jumper" :page-size="pageSize" :current-page="page" :total="total" @current-change="onPage" />
      </div>
    </el-card>

    <!-- 审核对话框 -->
    <el-dialog v-model="showAudit" title="审核学术活动" width="520px">
      <el-form :model="auditForm" label-width="80px">
        <el-form-item label="审核结果">
          <el-radio-group v-model="auditForm.status">
            <el-radio label="已通过">已通过</el-radio>
            <el-radio label="已驳回">已驳回</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审核意见">
          <el-input type="textarea" v-model="auditForm.opinion" rows="3"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="showAudit=false">取消</el-button>
        <el-button type="primary" :loading="auditLoading" @click="submitAudit">确定</el-button>
      </template>
    </el-dialog>

    <!-- 查看详情对话框 -->
    <el-dialog v-model="showDetail" title="学术活动详情" width="780px">
      <div v-if="current">
        <el-descriptions :column="2" border>
          <el-descriptions-item label="活动名称">{{ current.activityName }}</el-descriptions-item>
          <el-descriptions-item label="类型">{{ current.activityType }}</el-descriptions-item>
          <el-descriptions-item label="主办单位">{{ current.organizer }}</el-descriptions-item>
          <el-descriptions-item label="主办级别">{{ current.organizerLevel || '-' }}</el-descriptions-item>
          <el-descriptions-item label="开始日期">{{ d(current.startDate) }}</el-descriptions-item>
          <el-descriptions-item label="结束日期">{{ d(current.endDate) }}</el-descriptions-item>
          <el-descriptions-item label="参与人">{{ current.participantName }}</el-descriptions-item>
          <el-descriptions-item label="参与角色">{{ current.participantRole }}</el-descriptions-item>
          <el-descriptions-item label="是否报告">{{ current.hasReport ? '是' : '否' }}</el-descriptions-item>
          <el-descriptions-item label="报告题目">{{ current.reportTitle || '-' }}</el-descriptions-item>
          <el-descriptions-item label="审核状态">{{ statusText(current.status) }}</el-descriptions-item>
          <el-descriptions-item label="审核意见">{{ current.reviewComment || '-' }}</el-descriptions-item>
        </el-descriptions>

        <el-divider content-position="left">证明材料</el-divider>
        <el-table :data="attachments" size="small" stripe>
          <el-table-column prop="fileName" label="文件名" min-width="240" />
          <el-table-column prop="fileType" label="类型" width="120" />
          <el-table-column prop="contentType" label="MIME" width="160" />
          <el-table-column prop="fileSize" label="大小" width="120">
            <template #default="{ row }">{{ formatSize(row.fileSize) }}</template>
          </el-table-column>
          <el-table-column label="操作" width="160">
            <template #default="{ row }">
              <a class="el-link el-link--primary" :href="row.filePath" target="_blank">下载</a>
              <el-divider direction="vertical" />
              <el-button type="text" style="color:#f56c6c;" @click="deleteAtt(row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <template #footer>
        <el-button type="primary" @click="showDetail=false">关闭</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import activitiesApi from '@/api/activities'

const q = ref({ type: '', role: '', organizerLevel: '', range: [] })
const page = ref(1)
const pageSize = ref(10)
const total = ref(0)
const rows = ref([])
const stats = ref({ total: 0, pending: 0, rejected: 0, approved: 0 })
const loading = ref(false)

const showAudit = ref(false)
const auditForm = ref({ id: null, status: '已通过', opinion: '' })
const auditLoading = ref(false)

const showDetail = ref(false)
const current = ref(null)
const attachments = ref([])

function params() {
  return {
    page: page.value,
    size: pageSize.value,
    type: q.value.type || undefined,
    role: q.value.role || undefined,
    organizerLevel: q.value.organizerLevel || undefined,
    startDate: q.value.range?.[0] || undefined,
    endDate: q.value.range?.[1] || undefined
  }
}

function d(v) { return String(v || '').substring(0,10) }
function statusText(s) {
  if (!s) return '审核中'
  if (s === 'APPROVED' || s === '已通过') return '已通过'
  if (s === 'REJECTED' || s === '已驳回') return '已驳回'
  return '审核中'
}
function tag(s) { const t=statusText(s); return t==='已通过'?'success':(t==='已驳回'?'danger':'info') }
function formatSize(size){ if(!size&&size!==0) return '-'; if(size<1024) return size+' B'; if(size<1024*1024) return (size/1024).toFixed(1)+' KB'; return (size/1024/1024).toFixed(1)+' MB' }

async function fetchAll() {
  try {
    loading.value = true
    const res = await activitiesApi.list(params())
    rows.value = res?.content || []
    total.value = res?.totalElements || 0
    stats.value = await activitiesApi.stats(params())
  } catch (e) {} finally { loading.value = false }
}

function onPage(p) { page.value = p; fetchAll() }

function openAudit(row, defaultStatus) {
  auditForm.value = { id: row.id, status: defaultStatus || '已通过', opinion: '' }
  showAudit.value = true
}

async function submitAudit() {
  try {
    auditLoading.value = true
    await activitiesApi.review(auditForm.value.id, { status: auditForm.value.status, opinion: auditForm.value.opinion })
    ElMessage.success('审核完成')
    showAudit.value = false
    fetchAll()
  } catch (e) {} finally { auditLoading.value = false }
}

async function view(row) {
  try {
    current.value = await activitiesApi.getById(row.id)
    attachments.value = await activitiesApi.listAttachments(row.id)
    showDetail.value = true
  } catch (e) {}
}

async function deleteAtt(att){
  try{
    await ElMessageBox.confirm(`删除附件“${att.fileName}”？`, '确认', { type:'warning' })
    await activitiesApi.deleteAttachment(att.id)
    attachments.value = attachments.value.filter(a=>a.id!==att.id)
    ElMessage.success('已删除')
  }catch{}
}

onMounted(fetchAll)
</script>

<style scoped>
.activity-admin { max-width: 1400px; }
.page-header { display:flex; justify-content:space-between; align-items:center; margin-bottom:16px; }
.filters { display:flex; align-items:center; }
.stats-row { margin-bottom: 16px; }
.stat { text-align:center; }
.num { font-size: 24px; font-weight:600; }
.num.success { color:#67c23a; }
.num.danger { color:#f56c6c; }
.num.info { color:#909399; }
</style> 